<?php
/**
 * This file is part of Task Agile.
 * Task Agile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * Task Agile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Task Agile.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   TaskAgile
 * @package    Controller
 * @copyright  Copyright (c) 20010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @license    http://www.gnu.org/licenses/gpl.txt GPLv3
 * @version    $Id$
*/
class Task_ProjectController extends Tev_Controller_Crud
{
    public function listAction()
    {
        parent::listAction();

        // Enterprises
        $enterprise = new Enterprise();
        $rowSetEnterprises = $enterprise->fetchAll(null, 'name');
        unset($enterprise);
        $enterprises = array();
        foreach ($rowSetEnterprises as $rowEnterprise) {
            $enterprises[$rowEnterprise->id] = $rowEnterprise->name;
        }
        $this->view->enterprises = $enterprises;
        unset($enterprises);
    }

    public function formAction()
    {
        // Enterprises
        $enterprise = new Enterprise();
        $rowSetEnterprises = $enterprise->fetchAll(null, 'name');

        $enterprises = array();
        foreach ($rowSetEnterprises as $rowEnterprise) {
            $enterprises[$rowEnterprise->id] = $rowEnterprise->name;
        }
        $this->view->enterprises = $enterprises;
        unset($enterprises);

        $project = new Project();
        if ($id = $this->_getParam('id', false)) {
            $rowProject = $project->find($id)->current();
        } else {
            $rowProject = $project->createRow();
        }

        $this->view->project = $rowProject;

        $task = new Task();
        $this->view->prioritys = $task->getPrioritys();
        $this->view->status    = $task->getStatus();

        if ($rowProject->fk_enterprise) {
            $rowEnterprise = $enterprise->find($rowProject->fk_enterprise)->current();
            $rowSetUsers   = $rowEnterprise->findUserViaEnterpriseUser($enterprise->select()->order('name'));

            $users = array();
            foreach ($rowSetUsers as $rowUser) {
                $users[$rowUser->id] = $rowUser->name;
            }
            $this->view->users = $users;
            unset($users);

            // project_user
            $usersSelected = array();
            $rowSetProjectUser = $rowProject->findProjectUser();
            foreach ($rowSetProjectUser as $rowProjectUser) {
                $usersSelected[] = $rowProjectUser->fk_user;
            }
            $this->view->usersSelected = $usersSelected;
        }
    }

    public function saveAction()
    {
        $project = new Project();
        if ($id = $this->_getParam('id', false)) {
            $rowProject = $project->find($id)->current();
            $rowProject->status = $this->_getParam('status', Project::STATUS_ACTIVE);
        } else {
            $rowProject = $project->createRow();
            $rowProject->dt_register = date('Y-m-d H:i:s');
            $rowProject->status      = Project::STATUS_ACTIVE;
        }
        $rowProject->name = $this->_getParam('name');
        $rowProject->fk_enterprise = $this->_getParam('fk_enterprise');
        $rowProject->priority      = $this->_getParam('priority');
        $idProject = $rowProject->save();

        // project_user
        $projectUser = new ProjectUser();
        // @todo ver como tratar esse caso
        $projectUser->delete('fk_project = ' . $idProject);
        $users = $this->_getParam('users', array());
        foreach ($users as $idUser) {
            $row = $projectUser->createRow();
            $row->fk_user = $idUser;
            $row->fk_project = $idProject;
            $row->save();
        }
        $this->_redirect('task/project/list');
    }
}
